package info.grid.despierto.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * GroupTable用データアクセスクラス
 */
public class GroupTableDao {

	private DatabaseOpenHelper helper = null;

	public GroupTableDao(Context context) {
		helper = new DatabaseOpenHelper(context);
	}

	/**
	 * Groupの保存 rowidがnullの場合はinsert、rowidが!nullの場合はupdate
	 *
	 * @param groupTable
	 *            保存対象のオブジェクト
	 * @return 保存結果
	 */
	public GroupTable save(GroupTable groupTable) {
		SQLiteDatabase db = helper.getWritableDatabase();
		GroupTable result = null;
		try {
			ContentValues values = new ContentValues();
			values.put(GroupTable.COLUMN_GROUP_NAME, groupTable.getGroupName());
			values.put(GroupTable.COLUMN_GROUP_ONOFF,groupTable.getGroupOnoff());
			values.put(GroupTable.COLUMN_REPEAT_EVERYDAY, groupTable.getRepeatEveryday());
			values.put(GroupTable.COLUMN_REPEAT_SUN, groupTable.getRepeatSun());
			values.put(GroupTable.COLUMN_REPEAT_MON, groupTable.getRepeatMon());
			values.put(GroupTable.COLUMN_REPEAT_TUE, groupTable.getRepeatTue());
			values.put(GroupTable.COLUMN_REPEAT_WED, groupTable.getRepeatWed());
			values.put(GroupTable.COLUMN_REPEAT_THU, groupTable.getRepeatThu());
			values.put(GroupTable.COLUMN_REPEAT_FRI, groupTable.getRepeatFri());
			values.put(GroupTable.COLUMN_REPEAT_SAT, groupTable.getRepeatSat());


			Long rowId = groupTable.getRowid();
			// IDがnullの場合はinsert
			if (rowId == null) {
				rowId = db.insert(GroupTable.TABLE_NAME, null, values);
			} else {
				db.update(GroupTable.TABLE_NAME, values, GroupTable.COLUMN_ID
						+ "=?", new String[] { String.valueOf(rowId) });
			}
			result = load(rowId);
		} finally {
			db.close();
		}
		return result;
	}

	/**
	 * レコードの削除
	 *
	 * @param groupTable
	 *            削除対象のオブジェクト
	 */
	public void delete(GroupTable groupTable) {
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			db.delete(GroupTable.TABLE_NAME, GroupTable.COLUMN_ID + "=?",new String[] { String.valueOf(groupTable.getRowid()) });
		} finally {
			db.close();
		}
	}

	/**
	 * idでGroupTableをロードする
	 *
	 * @param rowId
	 *            PK
	 * @return ロード結果
	 */
	public GroupTable load(Long rowId) {
		SQLiteDatabase db = helper.getReadableDatabase();

		GroupTable groupTable = null;
		try {
			Cursor cursor = db.query(GroupTable.TABLE_NAME, null,
					GroupTable.COLUMN_ID + "=?",
					new String[] { String.valueOf(rowId) }, null, null, null);
			cursor.moveToFirst();
			groupTable = getGroupTable(cursor);
		} finally {
			db.close();
		}
		return groupTable;
	}

	/**
	 * 一覧を取得する
	 *
	 * @return 検索結果
	 */
	public List<GroupTable> list() {
		SQLiteDatabase db = helper.getReadableDatabase();

		List<GroupTable> groupTableList;
		try {
			Cursor cursor = db.query(GroupTable.TABLE_NAME, null, null, null,
					null, null, GroupTable.COLUMN_ID);
			groupTableList = new ArrayList<GroupTable>();
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				groupTableList.add(getGroupTable(cursor));
				cursor.moveToNext();
			}
		} finally {
			db.close();
		}
		return groupTableList;
	}

	/**
	 * カーソルからオブジェクトへの変換
	 *
	 * @param cursor
	 *            カーソル
	 * @return 変換結果
	 */
	private GroupTable getGroupTable(Cursor cursor) {
		GroupTable groupTable = new GroupTable();

		groupTable.setRowid(cursor.getLong(0));
		groupTable.setGroupName(cursor.getString(1));
		groupTable.setGroupOnoff(cursor.getInt(2));
		groupTable.setRepeatEveryday(cursor.getInt(3));
		groupTable.setRepeatSun(cursor.getInt(4));
		groupTable.setRepeatMon(cursor.getInt(5));
		groupTable.setRepeatTue(cursor.getInt(6));
		groupTable.setRepeatWed(cursor.getInt(7));
		groupTable.setRepeatThu(cursor.getInt(8));
		groupTable.setRepeatFri(cursor.getInt(9));
		groupTable.setRepeatSat(cursor.getInt(10));

		return groupTable;
	}
}
